package com.yy.system.action;

import com.yy.base.common.action.BaseCtrl;
import com.yy.base.log.Logger;
import com.yy.base.log.LoggerFactory;
import com.yy.base.page.Message;
import com.yy.base.util.ObjectUtil;
import com.yy.base.util.StringUtil;
import com.yy.system.entity.MenuInfo;
import com.yy.system.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/menuAction")
@SuppressWarnings("unchecked")
public class MenuAction extends BaseCtrl {
	private Logger log = LoggerFactory.getLogger(MenuAction.class);
	@Autowired
	private MenusService service;
	/**
	 * 查询所有菜单
	 */
	@ResponseBody
	@RequestMapping(value="/queryMenuList.html")
	public Message queryMenuList() {
		Message message = new Message();
		try {
			List list = service.queryMenuList();
			message.formartData(ObjectUtil.listEntityToMap(list));
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("查询异常！");
		}
		return message;
	}
	/**
	 * 根据ID查询菜单
	 */
	@ResponseBody
	@RequestMapping(value="/selectMenus.html")
	public Message selectMenus(HttpServletRequest request) {
		MenuInfo minfo = ObjectUtil.mapToEntity(MenuInfo.class, this.getParameterMap(request));
		Message message = new Message();
		try {
			message.formartData(service.selectMenu(minfo.getId()));
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("查询异常！");
		}
		return message;
	}

	//添加菜单
	@ResponseBody
	@RequestMapping(value="/saveMenu.html")
	public Message saveMenu(HttpServletRequest request) {
		MenuInfo minfo = ObjectUtil.mapToEntity(MenuInfo.class, this.getParameterMap(request));
		Message message = new Message();
		try {
			if(StringUtil.isNotEmpty(minfo.getId())) {
				service.update(minfo, "t_menu");
				message.setOkMessage("保存成功！");
			} else {
				service.save(minfo, "t_menu");
				message.setOkMessage("保存成功！");
			}
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("保存异常！");
		}
		return message;
	}
	/**
	 * 删除菜单
	 */
	@ResponseBody
	@RequestMapping(value="/deleteMenu.html")
	public Message deleteMenu(HttpServletRequest request) {
		String id = request.getParameter("id");
		Message message = new Message();
		try {
			if(StringUtil.isNotEmpty(id)){
				service.executeSql("delete emes_pda_Menu where emes_pda_Menu_id = ?", id);
				message.setOkMessage("删除成功！");
			}
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("删除异常！");
		}
		return message;
	}
}